package com.ywqf_api.service.impl;


import com.ywqf_api.base.BaseException;
import com.ywqf_api.dao.MobileDao;
import com.ywqf_api.dto.in.*;
import com.ywqf_api.entity.*;
import com.ywqf_api.service.MobileService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/3/28 0028.
 */
@Service
public class MobileServiceImpl implements MobileService {
    @Autowired
    private MobileDao mobileDao;


    /**
     * 注册接口
     *
     * @param phone
     * @param password
     * @return
     */
    @Override
    public int saveLogin(String phone, String password) {
        int save = mobileDao.saveLogin(phone, password);
        if (save > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public Map getUserInfo(UserLoginDto userLoginDto) {
        Integer type = userLoginDto.getType();
        if (type == null) {
            return null;
        }
        Map map = new HashMap();
        Customer customer = mobileDao.getUserInfo(userLoginDto);
        Integer houseId = customer.getHouseId();
        House house = mobileDao.getUserHouse(houseId);
        if (house == null) {
            map.put("user", customer);
            map.put("house", null);
            map.put("cars", null);
            map.put("community", null);
            return map;
        }
        List<House> isRentSale = mobileDao.findIsRentSale(houseId);
        house.setIsRent(0);
        house.setIsSale(0);
        if (isRentSale.size() > 0) {
            for (int i = 0; i < isRentSale.size(); i++) {
                House is = isRentSale.get(i);
                Integer types = is.getType();
                if (types == 3) {
                    house.setIsRent(1);
                    house.setIsSale(1);
                } else if (types == 1) {
                    house.setIsSale(1);
                } else {
                    house.setIsRent(1);
                }
            }
        }
        Integer communityId = house.getCommunityId();
        CommunityCorps communityCorps = mobileDao.findCommCorp(communityId);
        if (communityCorps == null) {
            map.put("user", customer);
            map.put("house", house);
            map.put("cars", null);
            map.put("community", null);
            return map;
        }
        List<ParkingData> list = mobileDao.getUserParking(houseId);
        if (list.size() > 0) {
            map.put("user", customer);
            map.put("house", house);
            map.put("cars", list);
            map.put("community", communityCorps);
            customer.setCommunityId(communityCorps.getCommunityId());
            return map;
        } else {
            map.put("user", customer);
            map.put("house", house);
            map.put("cars", null);
            map.put("community", communityCorps);
            customer.setCommunityId(communityCorps.getCommunityId());
            return map;
        }
    }

    @Override
    public List<Banner> findBannerList(Integer communityId) {
        List<Banner> list = mobileDao.findBannerList(communityId);
        if (list.size() > 0) {
            for(int i=0;i<list.size();i++){
                Banner banner = list.get(i);
                if(banner.getBeginTime().length() >=19){
                    banner.setBeginTime(banner.getBeginTime().substring(0,19));
                }
               if(banner.getEndTime().length() >=19){
                   banner.setEndTime(banner.getEndTime().substring(0,19));
               }
            }
            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<CommunityCorps> findCommunityCorp(String search) {
        List<CommunityCorps> list = mobileDao.findCommunityCorp(search);
        if (list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public List<HouseIdAndNum> findHouseIdNum(String search, Integer communityId) {
        List<HouseIdAndNum> list = mobileDao.findHouseIdNum(search, communityId);
        if (list.size() > 0) {
            return list;
        } else {
            return null;
        }
    }

    @Override
    public int saveComplaint(Integer customerId, String content) {
        int save = mobileDao.saveComplaint(customerId, content);
        if (save > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public List findRoleList(Integer communityId) {
        List<RoleDept> roleDepts = mobileDao.findRoleList(communityId);
        List list = new ArrayList();
        if (roleDepts.size() > 0) {
            for (int i = 0; i < roleDepts.size(); i++) {
                Map map = new HashMap();
                RoleDept find = roleDepts.get(i);
                Integer deptId = find.getDeptId();
                map.put("deptId", deptId);
                map.put("deptName", find.getDeptName());
                map.put("parentId",String.valueOf(find.getParentId()));
                List<UserRoleDept> roleDepts1 = mobileDao.findUserList(deptId, communityId);
                map.put("user", roleDepts1);
                list.add(i, map);
            }
            return list;
        } else {
            return null;
        }
    }

    @Override
    public Map findQuestionnaire(Integer communityId) {
        Map map = new HashMap();  //大map
        Questionnaire questionnaire = mobileDao.findQuestionnaire(communityId);
        if (questionnaire != null) {
            map.put("questionnaireId", questionnaire.getQuestionnaireId());
            map.put("questionnaireName", questionnaire.getQuestionnaireName());
            map.put("communityId", questionnaire.getCommunityId());
            if(questionnaire.getBeginTime().length() >=19){
                questionnaire.setBeginTime(questionnaire.getBeginTime().substring(0,19));
            }
            map.put("beginTime", questionnaire.getBeginTime());
            if(questionnaire.getEndTime().length() >=19){
                questionnaire.setEndTime(questionnaire.getEndTime().substring(0,19));
            }
            map.put("endTime", questionnaire.getEndTime());
            List<Questionnaire> list = mobileDao.findQuestionnaireList(questionnaire.getQuestionnaireId());
            if (list.size() > 0) {
                List listPro = new ArrayList();   ////////问题list
                for (int i = 0; i < list.size(); i++) {
                    Questionnaire problem = list.get(i);
                    Map mapPro = new HashMap();     /////问题map
                    mapPro.put("title", problem.getQuestionTitle());
                    mapPro.put("type", problem.getQuestionType());
                    mapPro.put("sort", problem.getQuestionSort());
                    List<Questionnaire> option = mobileDao.findOption(questionnaire.getQuestionnaireId(), problem.getQuestionSort());
                    mapPro.put("option", option);
                    listPro.add(mapPro);
                }
                map.put("question", listPro);
            }
            return map;
        }
        return null;
    }

    @Override
    public int findQuestionnaireCustomer(Integer customerId, Integer questionId) {
        Integer count  = mobileDao.findQuestionnaireCustomer(customerId,questionId);
        if(count >0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    @Transactional
    public int saveQuestionnaire(Integer customerId, Integer questionId, HttpServletRequest request) {
        String[] proid = request.getParameterValues("form");
        JSONArray batchList = JSONArray.fromObject(proid[0]);
        for (int o = 0; o < batchList.size(); o++) {
            JSONObject myJson = (JSONObject) batchList.get(o);
            Map m = myJson;
            String name = (String) m.get("name");
            int id = Integer.parseInt(name);
            String value = (String) m.get("value");
            int success = mobileDao.saveQuestionnaire(id, value, customerId, questionId);
            if (success <= 0) {
                throw new BaseException("提交答案失败");
            }
        }
        return 1;
    }


    @Override
    public int findCustomerId(String phone) {
        String customId = mobileDao.findCustomerId(phone);
        if (customId == null) {
            return 0;
        }
        return 1;
    }

    @Override
    public int getUserInfos(String telephone, String password) {
        Customer customId = mobileDao.findCustomer(telephone);
        if (customId == null) {
            return 0;
        }
        String pwd = customId.getPassword();
        if (pwd.equals(password)) {
            return 1;
        } else {
            return -1;
        }
    }

    @Override
    public int getUserInfos(String telephone) {
        Customer customId = mobileDao.findCustomer(telephone);
        if (customId == null) {
            return 0;
        }else {
            return 1;
        }
    }


    @Override
    public Water findWaterFeeTimeAndCorp(Integer houseId, Integer communityId) {
        Water water = mobileDao.findWaterFeeTimeAndCorp(houseId, communityId);
        if (water == null) {
            return null;
        } else {
            if(water.getPayTime().length() >=19){
                water.setPayTime(water.getPayTime().substring(0,19));
            }
            return water;
        }
    }

    @Override
    public Water findCorp(Integer communityId) {
        Water water = mobileDao.findCorp(communityId);
        return water;
    }

    @Override
    public Electricity findElectricityFeeTimeAndCorpAndHouseNum(Integer houseId, Integer communityId) {
        Electricity electricity = mobileDao.findElectricityFeeTimeAndCorpAndHouseNum(houseId, communityId);
        if (electricity == null) {
            return null;
        } else {
            if(electricity.getPayTime().length() >=19){
                electricity.setPayTime(electricity.getPayTime().substring(0,19));
            }
            return electricity;
        }
    }

    @Override
    public Map findHeatingFeeData(Integer houseId, Integer communityId) {
        Map map = new HashMap();
        Heating date = mobileDao.findHeatingDate(houseId);   //查询上次缴费时间
        Heating heating = mobileDao.findHeatingData(houseId, communityId);
        if (heating == null) {
            return null;
        }
        if (date != null) {
            heating.setYear(date.getYear());
            if(date.getPaymentDate().length() >= 19){
                heating.setPaymentDate(date.getPaymentDate().substring(0,19));
            }else{
                heating.setPaymentDate(date.getPaymentDate());
            }
        }
        List<Heating> list = mobileDao.findHeatingDisc(communityId);
        map.put("data", heating);
        map.put("disc", list);
        DiscountMessage discountMessage = mobileDao.findCommunityDiscountMessage(communityId);
        if (discountMessage != null) {
            map.put("discMessage", discountMessage);
        }
        return map;
    }

    @Override
    public Map findEstateFeeData(Integer houseId, Integer communityId) {
        Map map = new HashMap();
        EstateData date = mobileDao.findEstateDate(houseId);
        EstateData estate = mobileDao.findEstateData(houseId, communityId);
        if (estate == null) {
            return null;
        }
        if (date != null) {
            if(date.getPayDate().length()>=19){
                estate.setPayDate(date.getPayDate().substring(0,19));
            }else{
                estate.setPayDate(date.getPayDate());
            }
            if(date.getServiceStartDate().length() >=19){
                estate.setServiceStartDate(date.getServiceStartDate().substring(0,19));
            }else{
                estate.setServiceStartDate(date.getServiceStartDate());
            }
            if(date.getServiceEndDate().length() >=19){
                estate.setServiceEndDate(date.getServiceEndDate().substring(0,19));
            }else{
                estate.setServiceEndDate(date.getServiceEndDate());
            }
        }
        List<EstateData> list = mobileDao.findEstateDisc(communityId);
        map.put("data", estate);
        map.put("disc", list);
        DiscountMessage discountMessage = mobileDao.findCommunityDiscountMessage(communityId);
        if (discountMessage != null) {
            map.put("discMessage", discountMessage);
        }
        return map;
    }

    @Override
    public Map findParkingFeeData(Integer houseId, Integer communityId, Integer parkingSpaceId) {
        Map map = new HashMap();
        ParkingData date = mobileDao.findParkingDate(houseId, parkingSpaceId);
        ParkingData parking = mobileDao.findParkingData(houseId, communityId, parkingSpaceId);
        if (parking == null) {
            return null;
        }
        if (date != null) {
            if(date.getPayDate().length()>=19){
                parking.setPayDate(date.getPayDate().substring(0,19));
            }else{
                parking.setPayDate(date.getPayDate());
            }
            if(date.getServiceStartDate().length() >=19){
                parking.setServiceStartDate(date.getServiceStartDate().substring(0,19));
            }else{
                parking.setServiceStartDate(date.getServiceStartDate());
            }
            if(date.getServiceEndDate().length() >=19){
                parking.setServiceEndDate(date.getServiceEndDate().substring(0,19));
            }else{
                parking.setServiceEndDate(date.getServiceEndDate());
            }

        }
        List<ParkingData> list = mobileDao.findParkingDisc(communityId);
        map.put("data", parking);
        map.put("disc", list);
        DiscountMessage discountMessage = mobileDao.findCommunityDiscountMessage(communityId);
        if (discountMessage != null) {
            map.put("discMessage", discountMessage);
        }
        return map;
    }

    /**
     * 水费缴费接口
     *
     * @param waterFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveWaterFee(WaterFeeDto waterFeeDto) {
        int identification = 1;
        order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        if (good == null) {
            orderNum = "W" + longstr2 + "00001";
        } else {
            long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
            if (aa == longstr2) {
                long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                bb = bb + 1;
                String billNumber = String.valueOf(bb);
                String[] ss = {"00000", "0000", "000", "00", "0"};
                billNumber = ss[billNumber.length()] + billNumber;
                orderNum = "W" + longstr2 + billNumber;
            } else {
                orderNum = "W" + longstr2 + "00001";
            }
        }

        waterFeeDto.setOrderNum(orderNum);
        int order = mobileDao.saveWaterOrder(waterFeeDto);
        if (order > 0) {
            int orderGood = mobileDao.saveWaterOrderGood(waterFeeDto);
            if (orderGood > 0) {
                Integer orderId = waterFeeDto.getOrderId();
                return orderId;
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    @Transactional
    public int saveWaterFees(WaterFeeDto waterFeeDto) {
        int water = mobileDao.saveWater(waterFeeDto);
                if (water > 0) {
                    Integer orderId = waterFeeDto.getOrderId();
                    Integer waterId = waterFeeDto.getWaterPayRecordId();
                    int update = mobileDao.updateOrderWater(waterId, orderId);
                    if (update > 0) {
                        return orderId;
                    } else {
                        throw new BaseException("添加失败");
                    }
                } else {
                    throw new BaseException("添加失败");
                }
    }

    /**
     * 电费缴费接口
     *
     * @param houseId
     * @param houseId
     * @return
     */
    @Override
    @Transactional
    public int findElectricityHouse(Integer houseId) {

        Electricity electricity = mobileDao.findElectricityHouse(houseId);
        if (electricity == null) {
            return 0;
        }else {
            return 1;
        }
    }
    /**
     * 电费缴费接口
     *
     * @param electricityFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveElectricityFee(ElectricityFeeDto electricityFeeDto) {
        int identification = 2;
        order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        if (good == null) {
            orderNum = "E" + longstr2 + "00001";
        } else {
            long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
            if (aa == longstr2) {
                long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                bb = bb + 1;
                String billNumber = String.valueOf(bb);
                String[] ss = {"00000", "0000", "000", "00", "0"};
                billNumber = ss[billNumber.length()] + billNumber;
                orderNum = "E" + longstr2 + billNumber;
            } else {
                orderNum = "E" + longstr2 + "00001";
            }
        }
        electricityFeeDto.setOrderNum(orderNum);
        int order = mobileDao.saveElectricityOrder(electricityFeeDto);
        if (order > 0) {
            int orderGood = mobileDao.saveElectricityOrderGood(electricityFeeDto);
            if (orderGood > 0) {
                return electricityFeeDto.getOrderId();
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }
    /**
     * 电费缴费接口
     *
     * @param electricityFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveElectricityFees(ElectricityFeeDto electricityFeeDto) {
        Integer houseId = electricityFeeDto.getHouseId();
        Electricity electricity = mobileDao.findElectricityHouse(houseId);
        if (electricity == null) {
            throw new BaseException("添加失败");
        } else {
            Integer eleId = electricity.getElectricityMeterId();
            electricityFeeDto.setElectricityMeterId(eleId);
            int electricitys = mobileDao.saveElectricity(electricityFeeDto);
            if (electricitys > 0) {
                Integer orderId = electricityFeeDto.getOrderId();
                Integer electId = electricityFeeDto.getElectricityPayRecordId();
                int update = mobileDao.updateOrderElect(electId, orderId);
                if (update > 0) {
                    return orderId;
                } else {
                    throw new BaseException("添加失败");
                }
            } else {
                throw new BaseException("添加失败");
            }
        }
    }
    /**
     * 采暖费缴费接口
     *
     * @param heatingFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveHeatingFee(HeatingFeeDto heatingFeeDto) {
        int identification = 3;
        order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        if (good == null) {
            orderNum = "HW" + longstr2 + "00001";
        } else {
            String num = good.getOrderNum();
            long aa = Long.valueOf(good.getOrderNum().substring(2, 8));
            if (aa == longstr2) {
                long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                bb = bb + 1;
                String billNumber = String.valueOf(bb);
                String[] ss = {"00000", "0000", "000", "00", "0"};
                billNumber = ss[billNumber.length() - 1] + billNumber;
                orderNum = "HW" + longstr2 + billNumber;
            } else {
                orderNum = "HW" + longstr2 + "00001";
            }
        }
        heatingFeeDto.setOrderNum(orderNum);
        int order = mobileDao.saveHeatingOrder(heatingFeeDto);
        if (order > 0) {
            System.out.println(heatingFeeDto.getOrderId());
            int orderGood = mobileDao.saveHeatingOrderGood(heatingFeeDto);
            if (orderGood > 0) {
                return heatingFeeDto.getOrderId();
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    @Transactional
    public int saveHeatingFees(HeatingFeeDto heatingFeeDto) {
        String num = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                num += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                num += String.valueOf(random.nextInt(10));
            }
        }
        heatingFeeDto.setHeatingPayNum(num);
        int heating = mobileDao.saveHeating(heatingFeeDto);
        if (heating > 0) {
            Integer orderId = heatingFeeDto.getOrderId();
            Integer heatingId = heatingFeeDto.getHeatingPayId();
            int update = mobileDao.updateOrderHeating(heatingId, orderId);
            if (update > 0) {
                return orderId;
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    /**
     * 车位费缴费接口
     *
     * @param parkingFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveParkingFee(ParkingFeeDto parkingFeeDto) {
        int identification = 4;
        order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        if (good == null) {
            orderNum = "P" + longstr2 + "00001";
        } else {
            long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
            if (aa == longstr2) {
                long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                bb = bb + 1;
                String billNumber = String.valueOf(bb);
                String[] ss = {"00000", "0000", "000", "00", "0"};
                billNumber = ss[billNumber.length()] + billNumber;
                orderNum = "P" + longstr2 + billNumber;
            } else {
                orderNum = "P" + longstr2 + "00001";
            }
        }
        parkingFeeDto.setOrderNum(orderNum);
        int order = mobileDao.saveParkingOrder(parkingFeeDto);
        if (order > 0) {
            int orderGood = mobileDao.saveParkingOrderGood(parkingFeeDto);
            if (orderGood > 0) {
                return parkingFeeDto.getOrderId();
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    @Transactional
    public int saveParkingFees(ParkingFeeDto parkingFeeDto) {
        String num = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                num += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                num += String.valueOf(random.nextInt(10));
            }
        }
        parkingFeeDto.setParkingPayNum(num);
        int heating = mobileDao.saveParking(parkingFeeDto);
        if (heating > 0) {
            Integer parkingSpaceId = parkingFeeDto.getParkingSpaceId();
            Integer parkingPayId = parkingFeeDto.getParkingPayId();
            String year = parkingFeeDto.getServiceStartDate().substring(0, 4);
            String month = parkingFeeDto.getServiceStartDate().substring(5, 7);
            Integer years = Integer.parseInt(year);
            Integer monthsd = Integer.parseInt(month);
            Integer months = monthsd - 1;
            Integer payMonth = parkingFeeDto.getPayMonth();
            for (int i = 0; i < payMonth; i++) {
                months = months + 1;
                if (months > 12) {
                    years = years + 1;
                    months = months - 12;
                    int est = mobileDao.saveParkingstatist(parkingPayId, parkingSpaceId, years, months);
                    if (est <= 0) {
                        throw new BaseException("添加失败");
                    }
                } else {
                    int est = mobileDao.saveParkingstatist(parkingPayId, parkingSpaceId, years, months);
                    if (est <= 0) {
                        throw new BaseException("添加失败");
                    }
                }
            }
            Integer orderId = parkingFeeDto.getOrderId();
            Integer parkingId = parkingFeeDto.getParkingPayId();
            int update = mobileDao.updateOrderParking(parkingId, orderId);
            if (update > 0) {
                return orderId;
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    /**
     * 物业费缴费接口
     *
     * @param estateFeeDto
     * @return
     */
    @Override
    @Transactional
    public int saveEstateFee(EstateFeeDto estateFeeDto) {
        int identification = 5;
        order good = mobileDao.findOrderGoodsInfo(identification); //查最新时间
        Date day = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long longstr2 = Long.valueOf(df.format(day).substring(2, 10).replaceAll("[-\\s:]", ""));
        String orderNum;
        if (good == null) {
            orderNum = "H" + longstr2 + "00001";
        } else {
            long aa = Long.valueOf(good.getOrderNum().substring(1, 7));
            if (aa == longstr2) {
                long bb = Long.valueOf(good.getOrderNum().substring(7, 12));
                bb = bb + 1;
                String billNumber = String.valueOf(bb);
                String[] ss = {"00000", "0000", "000", "00", "0"};
                billNumber = ss[billNumber.length()] + billNumber;
                orderNum = "H" + longstr2 + billNumber;
            } else {
                orderNum = "H" + longstr2 + "00001";
            }
        }
        estateFeeDto.setOrderNum(orderNum);
        int order = mobileDao.saveEstateOrder(estateFeeDto);
        if (order > 0) {
            System.out.println(estateFeeDto.getOrderId());
            int orderGood = mobileDao.saveEstateOrderGood(estateFeeDto);
            if (orderGood > 0) {
                return estateFeeDto.getOrderId();
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    public int saveEstateFees(EstateFeeDto estateFeeDto) {
        String num = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < 12; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                num += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                num += String.valueOf(random.nextInt(10));
            }
        }
        estateFeeDto.setEstatePayNum(num);
        int heating = mobileDao.saveEstate(estateFeeDto);
        if (heating > 0) {
            Integer houseId = estateFeeDto.getHouseId();
            Integer estatePayId = estateFeeDto.getEstatePayId();
            String year = estateFeeDto.getServiceStartDate().substring(0, 4);
            String month = estateFeeDto.getServiceStartDate().substring(5, 7);
            Integer years = Integer.parseInt(year);
            Integer monthsd = Integer.parseInt(month);
            Integer months = monthsd - 1;
            Integer payMonth = estateFeeDto.getPayMonth();
            for (int i = 0; i < payMonth; i++) {
                months = months + 1;
                if (months > 12) {
                    years = years + 1;
                    months = months - 12;
                    int est = mobileDao.saveEstatestatist(estatePayId, houseId, years, months);
                    if (est <= 0) {
                        throw new BaseException("添加失败");
                    }
                } else {
                    int est = mobileDao.saveEstatestatist(estatePayId, houseId, years, months);
                    if (est <= 0) {
                        throw new BaseException("添加失败");
                    }
                }
            }
            Integer orderId = estateFeeDto.getOrderId();
            int update = mobileDao.updateOrderEstate(estatePayId, orderId);
            if (update > 0) {
                return orderId;
            } else {
                throw new BaseException("添加失败");
            }
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    public List findEstateCalendar(Integer houseId) {
        List arrays = new ArrayList(); //空list
        List<EstateData> list = mobileDao.findEstateCalendar(houseId);
        if (list.size() == 0) {
            return null;
        }
        int[] months = null;  //定义数组
        int z = 0;  //增强for索引
        for (EstateData lt : list) {
            z++;
            Map map = new HashMap();
            months = new int[12];
            Integer year = lt.getYear();
            List<EstateData> find = mobileDao.findEstateMonthStatus(houseId, year);
            for (EstateData fin : find) {
                Integer num = fin.getMonth();
                months[num - 1] = 1;
            }
            List array = new ArrayList();
            for (int o = 0; o < months.length; o++) {
                array.add(months[o]);
            }
            map.put("year", year);
            map.put("month", array);
            arrays.add(z - 1, map);
        }
        return arrays;
    }


    @Override
    public List findParkingCalendar(Integer parkingSpaceId) {
        List arrays = new ArrayList(); //空list
        List<ParkingData> list = mobileDao.findParkingCalendar(parkingSpaceId);
        if (list.size() == 0) {
            return null;
        }
        int[] months = null;  //定义数组
        int z = 0;  //增强for索引
        for (ParkingData lt : list) {
            z++;
            Map map = new HashMap();
            months = new int[12];
            Integer year = lt.getYear();
            List<ParkingData> find = mobileDao.findParkingMonthStatus(parkingSpaceId, year);
            for (ParkingData fin : find) {
                Integer num = fin.getMonth();
                months[num - 1] = 1;
            }
            List array = new ArrayList();
            for (int o = 0; o < months.length; o++) {
                array.add(months[o]);
            }
            map.put("year", year);
            map.put("month", array);
            arrays.add(z - 1, map);
        }
        return arrays;
    }

    @Override
    public int updateOrderStatus(int orderId,String thirdNo) {
        return mobileDao.updateOrderStatus(orderId,thirdNo);
    }
}
